﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using TiEMIS;

namespace ModelingClient
{
    public partial class FormItemForm : Form
    {
        DataConfig config;
        string[] formCodes;
        bool isAdd = false;
        FormDefine formDefine;
        public FormItemForm()
        {
            InitializeComponent();
        }
        public void Init(DataConfig config)
        {
            this.config = config;
            panel.Controls.Clear();
            RefreshTree();
        }
        private void FormItemForm_Shown(object sender, EventArgs e)
        {
            config = new DataConfig();
            config.KeyField = "FieldCode";
            config.TitleField = "Title";
            config.FieldList.Add(new ItemData("FieldCode", "编码"));
            config.FieldList.Add(new ItemData("Title", "名称"));
            config.FieldList.Add(new ItemData("Description", "描述"));
            config.FieldList.Add(new ItemData("ED_Parent", "树型结构") { Type = DataType.Bool });
            config.FieldList.Add(new ItemData("ED_IsEnum", "是否为枚举字典") { Type = DataType.Bool });
            panel.Controls.Clear();
            RefreshTree();
        }
        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        void RefreshTree()
        {
            treeView.Nodes.Clear();
            foreach (var item in formDefine.Items)
            {
                TreeNode node = new TreeNode();
                node.Text = item.Title;
                node.Tag = item.FieldCode;
                treeView.Nodes.Add(node);
            }
        }
        void GenerateControls()
        {
            panel.Controls.Clear();
            foreach (ItemData item in config.FieldList)
            {
                Control control = null;
                if (item.Type == DataType.String)
                    control = new ItemTextControl(item.Name, item.Default as string);
                else if (item.Type == DataType.Number)
                    control = new ItemNumberControl(item.Name);
                else if (item.Type == DataType.Bool)
                    control = new ItemCheckboxControl(item.Name, item.Description);
                else if (item.Type == DataType.Enum)
                    control = new ItemEnumControl(item.Name, item.Enums);
                control.Tag = item;
                AddControl(control);
            }
        }
        void AddControl(Control control)
        {
            control.Width = panel.Width - 50;
            if (panel.Controls.Count > 0)
                control.Location = new Point(1, panel.Controls[panel.Controls.Count - 1].Location.Y + control.Height + 1);
            else
                control.Location = new Point(1, 1);
            panel.Controls.Add(control);
        }


        private void treeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            DataRow row = (DataRow)e.Node.Tag;
            GenerateControls();
            foreach (ItemBaseControl control in panel.Controls)
            {
                ItemData item = (ItemData)control.Tag;
                control.SetValue(row[item.Code]);
                if (item.Code == config.KeyField)
                    control.SetEnabled(false);
            }
        }
        private void AddMenuItem_Click(object sender, EventArgs e)
        {
            //Dictionary<string, object> dict = new Dictionary<string, object>();
            //dict["ED_Code"] = "sdfs";
            //dict["ED_Name"] = "fgjhfdj";
            //dict["ED_Description"] = "sadf";
            //dict["ED_Parent"] = null;
            //DbOperation.DB.Insert(config.TableName, dict);
            GenerateControls();
            isAdd = true;
        }
        private void SaveMenuItem_Click(object sender, EventArgs e)
        {
            if (isAdd)
            {
                Dictionary<string, object> dict = new Dictionary<string, object>();
                foreach (ItemBaseControl control in panel.Controls)
                {
                    ItemData item = (ItemData)control.Tag;
                    dict[item.Code] = control.GetValue();
                }
                foreach (var item in config.KeyDict)
                {
                    dict.Add(item.Key, item.Value);
                }
                DbOperation.DB.Insert(config.TableName, dict);
                if (AfterInsert != null)
                    AfterInsert(dict);
                panel.Controls.Clear();
                isAdd = false;
                AddControl(new Label() { Text = "添加成功" });
                RefreshTree();
            }
            else
            {
                Dictionary<string, object> dict = new Dictionary<string, object>();
                object key = null;
                foreach (ItemBaseControl control in panel.Controls)
                {
                    ItemData item = (ItemData)control.Tag;
                    if (item.Code == config.KeyField)
                        key = control.GetValue();
                    else
                        dict[item.Code] = control.GetValue();
                }
                dict.Add("\r", null);
                dict.Add(config.KeyField, key);
                foreach (var item in config.KeyDict)
                {
                    dict.Add(item.Key, item.Value);
                }
                DbOperation.DB.Update(config.TableName, dict);
                panel.Controls.Clear();
                AddControl(new Label() { Text = "修改成功" });
                RefreshTree();
            }
        }

        private void DeleteMenuItem_Click(object sender, EventArgs e)
        {
            if (this.treeView.SelectedNode == null)
                return;
            DataRow row = (DataRow)this.treeView.SelectedNode.Tag;
            string whereStr = "";
            foreach (var item in config.KeyDict)
            {
                if (item.Value is string)
                    whereStr += " and " + item.Key + " = '" + item.Value + "'";
                else
                    whereStr += " and " + item.Key + " = " + item.Value;
            }
            DbOperation.DB.ExecuteQuery("delete from " + config.TableName + " where " + config.KeyField + " = {0} " + whereStr, row[config.KeyField].ToString());
            if (AfterDelete != null)
                AfterDelete(row[config.KeyField].ToString());
            panel.Controls.Clear();
            AddControl(new Label() { Text = "删除成功" });
            RefreshTree();
        }

        

        
    }
}
